#include "config.h"
#include "web.h"
//处理nfc的数据
String inputBuffer = "";
bool stringComplete = false;
void getnfcdata() {
  while (Serial.available() > 0) {
    char inChar = (char)Serial.read();
    if (inChar == '\n') {
      stringComplete = true;
    } else {
      inputBuffer += inChar;
    }
  }
  // 处理完整字符串
  if (stringComplete) {
    StaticJsonDocument<200> doc;
    inputBuffer.trim(); // 去除首尾空白
    DeserializationError error = deserializeJson(doc, inputBuffer);
    //从nfc会员卡读取处理的数据格式如 {"sn":"asdfsafsdfasdfasdfsadfasdf"}
    if (error) {
      Serial.print("JSON解析错误: ");
      Serial.println(error.c_str());
    } else if (doc.containsKey("sn")) {
      //缓存会员卡数据
      const char* snid = doc["sn"];
      //这里马上请求网络获得用户数据
      strncpy(userConfig.snid, snid, sizeof(userConfig.snid) - 1);
      userConfig.snid[sizeof(userConfig.snid) - 1] = '\0'; // 确保字符串终止
      bool issetUserConfig = setUserConfig();
      //首次刷卡会先清理掉,再加载一次
      if (strcmp(userConfig.snid, snid) == 0 && issetUserConfig) {
        presskey(0, 1); //首次请求type=1
        Serial.print("长度: ");
        Serial.println(strlen(userConfig.snid));
        Serial.print("首次刷卡: ");
        Serial.println(userConfig.snid);
      }

      //clearUserConfig();//清除会员卡数据，在检测没有会员卡的时候执行
      Serial.print("snid: ");
      Serial.println(snid);
      // 进一步处理snid...
    }
    else {
      //{"addtick":25}
      Serial.println("JSON中缺少sn字段");
    }
    Serial.print("resive:");
    Serial.print(inputBuffer);
    Serial.println("");
    // 处理命令
    //processCommand(inputBuffer);

    // 清空缓冲区
    inputBuffer = "";
    stringComplete = false;
  }
  return;
}
